﻿// <com.woyouqiu.Copyright>
// --------------------------------------------------------------
// <copyright>上海有求网络科技有限公司 2015</copyright>
// <Solution>Vecen.BangChuang</Solution>
// <Project>Vecen.BangChuang.Core</Project>
// <FileName>EnumExtensions.cs</FileName>
// <CreateTime>2017-04-27 22:02</CreateTime>
// <Author>何苗</Author>
// <Email>hemiao@woyouqiu.com</Email>
// <log date="2017-04-27 22:02" version="00001">创建</log>
// --------------------------------------------------------------
// </com.woyouqiu.Copyright>

using System;
using System.ComponentModel;
using System.Reflection;

namespace Vecen.BangChuang.Enums {
    public static class EnumExtensions {
        /// <summary>
        /// 获得枚举备注
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Description(this Enum value) {
            var fieldInfo = value.GetType().GetField(value.ToString());
            if(fieldInfo == null) {
                return "";
            }
            var descriptionAttributes =
                fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];

            if(descriptionAttributes == null)
                return string.Empty;
            return (descriptionAttributes.Length > 0) ? descriptionAttributes[0].Description : value.ToString();
        }
    }
}